package com.tongzhan.service;


import com.tongzhan.entity.religion.Street;
import com.tongzhan.entity.religion.Village;
import com.tongzhan.repository.religion.StreetRepository;
import com.tongzhan.repository.religion.VillageRepository;
import com.tongzhan.vo.StreetVO;
import com.tongzhan.vo.VillageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class StreetService {

    @Autowired
    private StreetRepository streetRepository;

    @Autowired
    private VillageRepository villageRepository;

    public List<StreetVO> getStreetList() {
        //查询条件
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.ASC, "Sort"));
        //街道列表
        List<Street> streetList = streetRepository.findAll(query);
        //封装返回数据
        List<StreetVO> streetVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(streetList)) {
            for (Street street : streetList) {
                StreetVO streetVO = new StreetVO();
                streetVO.setStreetCode(street.getStreetCode());
                streetVO.setStreetName(street.getStreetName());
                streetVOS.add(streetVO);
            }
        }
        return streetVOS;
    }

    public List<VillageVO> getVillageList(String streetCode) {
        Query query = new Query();
        query.addCriteria(Criteria.where("StreetCode").is(streetCode));
        query.with(Sort.by(Sort.Direction.ASC, "VillageCode"));
        List<Village> villageList = villageRepository.findAll(query);
        //VO
        List<VillageVO> villageVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(villageList)) {
            for (Village village : villageList) {
                VillageVO vo = new VillageVO();
                vo.setVillageCode(village.getVillageCode());
                vo.setVillageName(village.getVillageName());
                villageVOS.add(vo);
            }
        }
        return villageVOS;
    }

}
